class QuestionListsController < ApplicationController
  before_filter :authenticate_user!
  before_filter do
    @competition_tab = 'question_lists'
  end
  
  # GET /question_lists
  # GET /question_lists.json
  def index
    @question_lists = QuestionList.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @question_lists }
    end
  end

  # GET /question_lists/1
  # GET /question_lists/1.json
  def show
    @question_list = QuestionList.find(params[:id])
    @competition = @question_list.competition
    @formula = true

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @question_list }
    end
  end

  # GET /question_lists/new
  # GET /question_lists/new.json
  def new
    @question_list = QuestionList.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @question_list }
    end
  end

  # GET /question_lists/1/edit
  def edit
    @question_list = QuestionList.find(params[:id])
  end

  # POST /question_lists.js
  def create
    @question_list = QuestionList.new(params[:question_list])
    @question_list.author = current_user
    @really_submit = params[:really_submit] != ''
    if @really_submit
      @question_list.save
    end
    respond_to do |format|
      #if @really_submit && @question_list.save
        format.js
        #format.html { redirect_to @question_list }
        #format.json { render json: @question_list, status: :created, location: @question_list }
      #else
      #  format.js
        #format.html { render action: "new" }
        #format.json { render json: @question_list.errors, status: :unprocessable_entity }
      #end
    end
  end

  # PUT /question_lists/1
  # PUT /question_lists/1.json
  def update
    @question_list = QuestionList.find(params[:id])

    respond_to do |format|
      if @question_list.update_attributes(params[:question_list])
        format.html { redirect_to @question_list, notice: 'Question list was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @question_list.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /question_lists/1
  # DELETE /question_lists/1.json
  def destroy
    @question_list = QuestionList.find(params[:id])
    @question_list.destroy

    respond_to do |format|
      format.html { redirect_to question_lists_url }
      format.json { head :no_content }
    end
  end

  # GET /question_lists/1/question.js
  def append
    @question_list = QuestionList.find(params[:id])
    @question = Question.new
    # @question.question_list = @question_list
    @question.set_question_list(@question_list)

    respond_to do |format|
      format.js
    end
  end

  # PUT /question_lists/1/publish
  def publish
    @question_list = QuestionList.find(params[:id])
    @question_list.published = true
    @question_list.save
    @question_list.topic.save
    respond_to do |format|
      format.html { redirect_to @question_list }
    end
  end

  # GET|PUT /question_lists/1/copy
  # GET|PUT /question_lists/1/copy.js
  def copy
    @question_list = QuestionList.find(params[:id])
    @competition = @question_list.competition

    @question_list_from = params[:from].to_s
    if @question_list_from.length > 0
      @question_list_from = QuestionList.find(@question_list_from)
    else
      @question_list_from = nil
    end

    @really_submit = !params[:really_submit].nil? && params[:really_submit] != ''
    if @really_submit
      params[:questions].each do |question_id|
        @question_list.clone_from(Question.find(question_id))
      end
    end

    respond_to do |format|
      format.html
      format.js
    end
  end
end
